package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.system.domain.UserChannel;
import com.ruoyi.system.mapper.UserChannelMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.UserChannelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserChannelServiceImpl extends BaseServiceImpl<UserChannelMapper, UserChannel> implements UserChannelService {

    @Resource
    private ISysUserService userService;

    public UserChannel getDomain(UserChannel userChannel) {

        userChannel = super.getDomain(userChannel);
        if (userChannel == null) {
            throw new BusiException("无通道权限");
        }
        return userChannel;
    }

//
//    public UserChannel updateKey(Long id) {
//        UserChannel userChannel = this.check(id);
//        // 给用户产生一个secertKey
//        String secertKey = UUID.randomUUID().toString().replaceAll("-", "");
//        userChannel.setSecertKey(secertKey);
//        this.updateDomain(userChannel);
//        return userChannel;
//    }


//    @Override
//    public UserChannel checkByUserId(Long userId) {
//        UserChannel userChannel = new UserChannel();
//        userChannel.setUserId(userId);
//        return this.getDomain(userChannel);
//    }

    @Override
    public UserChannel getOrderPayChannel(Long userId) {
        UserChannel userChannel=new UserChannel();
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageSize(1);
        pageDomain.setOrderByColumn("payRate");
        List<UserChannel> list = this.findPage(userChannel, pageDomain).getList();
        if(CollectionUtil.isEmpty(list)){
            throw new BusiException("通道已关闭或通道权限");
        }
        return list.get(0);
    }

    @Override
    public UserChannel checkByUserId(Long userId, String channelCode) {
        UserChannel userChannel = new UserChannel();
        userChannel.setUserId(userId);
        userChannel.setChannelCode(channelCode);
        return this.getDomain(userChannel);
    }


}
